<?php 
session_start();
require_once ("class/m_creditsale.php");

$m = new M_creditsale();
if (isset($_POST['m'])  &&  $_POST['m']!=""){
	if ($_POST['m']== 'search'){
		$page=$_POST['page'];
		$page=isset($page)?$page:1;	
		
		if ($_SESSION['branchtype'] =="1")
			$branchid=$_POST['barnchid'];
		else 
			$branchid=$_SESSION['bid'];
				
		$result=$m->getData($_POST['code'],$_POST['name'],$_POST['status'],$branchid,$page);
		$rows=$m->getRowCount();
		
		$i=1 + (($page-1) * $m->getListPage());
		
		$html="";
		if ($rows > 0){
			while($row = $result->FetchRow()){
				If($i%2 <> 0){
					$html .= "<tr class=row1>";
				} else{
					$html .= "<tr>";
				}
				$html .= genTable($row);
				$html .= "</tr>";
				$i++;
			}
		}else{
			$html="<tr>"
				."<td colspan='9' align='center' ><img src='./images/info.gif'/><b/></td>"
				."</tr>";
		}
		//Return result , rows , page
		$ar_result=array('data'=>$html,
						'rows'=>$rows,
						'totalpage'=>$m->getTotalPage());
		echo json_encode( $ar_result );
		
	}else if ($_POST['m']== 'detail'){
		$id=$_POST['id'];
		$result=$m->getDataDetail($id);
		$rows=$m->getRowCount();
		$html="";
		$i=1;
		$payTotal=0;
		if ($rows > 0){
			while($row = $result->FetchRow()){
				$html .= getTableDetail($row,$i);
				$i++;
				$payTotal += $row["Amount"];
			}
		}
		$arReturn=array('rows'=>$rows,
						'paytoal'=>number_format($payTotal,2,'.',','),
						'html'=> $html);
		echo json_encode($arReturn);
		
	}else if ($_POST['m']== 'add'){
		$id=$_POST['id'];
		$amt=$_POST['amt'];
		$type=$_POST['t'];
		$card=$_POST['c'];
		
		$ar=array('Credit_ID'=>$id,
					'PayDate'=>date( "Y/m/d H:i:s" ),
					'Amount'=>$amt,
					'PayType'=>$type,
					'CardNo'=>$card,
					'CreatedBy'=>$_SESSION['username']
					);
		//'CreatedDate'=>date( "Y/m/d H:i:s" ),
		$csid=$m->InsertCredit($ar);
		//update amount
		$m->updateCreditAmt($id,$amt,$_SESSION['username']) ;
		$result=$m->getDataDetail($id);
		$rows=$m->getRowCount();
		$html="";
		$i=1;
		$payTotal=0;
		if ($rows > 0){
			while($row = $result->FetchRow()){
				$html .= getTableDetail($row,$i);
				$i++;
				$payTotal += $row["Amount"];
			}
		}
		$arReturn=array('rows'=>$rows,
						'paytoal'=>number_format($payTotal,2,'.',','),
						'paydate'=>date( "d/m/Y" ),
						'html'=> $html
						,'csid'=>$csid);
		echo json_encode($arReturn);
		
	}else if ($_POST['m']== 'del'){
		$id=$_POST['id']; //credit_id
		$cid=$_POST['cid']; // id from tb_creditsale_detail
		$amt=$_POST['amt']; // Amount 
		//Update Total Pay
		$m->updateCreditAmt_reduce($id,$amt,$_SESSION['username']) ;
		//Delete Creditsale Detail
		$m->deleCredit($cid);
		//Get Last data
		$result=$m->getDataDetail($id);
		$rows=$m->getRowCount();
		$html="";
		$i=1;
		$payTotal=0;
		if ($rows > 0){
			while($row = $result->FetchRow()){
				$html .= getTableDetail($row,$i);
				$i++;
				$payTotal += $row["Amount"];
			}
		}
		$arReturn=array('rows'=>$rows,
						'paytoal'=>number_format($payTotal,2,'.',','),
						'html'=> $html);
		echo json_encode($arReturn);		
	}
}
function getTableDetail($row,$i){
				$html .= "<tr>";
				$html .= "<td align='right'>".$i."</td>";
				$html .= "<td style='display:none'>".$row["id"]."</td>";
				$html .= "<td align='center'>".$row["PayDate2"]."</td>";
				$html .= "<td align='center'>".$row["PayType"]."</td>";
				$html .= "<td align='center'>".$row["CardNo"]."</td>";
				$html .= "<td align='right'>".number_format($row["Amount"],2,'.',',')."</td>";
				
				$html .= "<td align='center'><img src='images/remove16.png' style='cursor:pointer' id='btnRemove' title='Delete' border=0/></td>";
				$html .= "</tr>";
				return $html;
}
function genTable($row){
	global $posScreen;
	if 	($row["RemainAmt"]=='0')
		$style='color:#999;';
	else 
		$style='color:red;';
			
	$s.="<td align='center'>".$row["Code"]."</td>" .
	"<td align='center' style='display:none;'>".$row["id"]."</td>" .
    "<td align='left' >".$row["BranchName"]."</td>" .
	"<td align='left'>".$row["FirstName"]." ".$row["LastName"]."</td>" .
	"<td align='center'>".$row["_buydate"]."</td>" .
	"<td align='center'>".$row["lastpay"]."</td>" .
	"<td align='right'>". number_format( $row["TotalAmt"],2,'.',',')."</td>" .
	"<td align='right' style='color:green'>".number_format($row["PayAmt"],2,'.',',')."</td>" .
	"<td align='right' style='".$style."'>".number_format($row["RemainAmt"],2,'.',',')."</td>" ;
	
	if ($_SESSION["permission"][$posScreen]["Add"]=="1") { 
		$s.="<td align='center'><img src='./images/creditsale_icon.png' class='btnPay' style='cursor:pointer' title='Pay & History ' border='0'/></td>";
	}else{
		$s.="<td align='center'> </td>";
	}
	return $s;
}
?>